<template>
  <div v-if="renderComponent" class="wrap">
    <tree-select
      v-bind="$attrs"
      :data="userOptions"
      :default-expand-keys="defaultExpandKeys"
      :default-expand-all="true"
      node-key="id"
      @input="$emit('input', $event)"
      @change="$emit('change', $event)"
    />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { treeToArray } from '@/utils'
import TreeSelect from '../TreeSelect'
export default {
  name: 'UserSelect',
  components: {
    [TreeSelect.name]: TreeSelect
  },
  data() {
    return {
      renderComponent: true
    }
  },
  computed: {
    ...mapState({
      userOptions: (state) => state.user.userTree
    }),
    defaultExpandKeys() {
      return treeToArray(this.userOptions)
        .map((item) => (item.open ? item.id : null))
        .filter((item) => item)
    }
  },
  mounted() {
    this.forceRerender()
  },
  methods: {
    forceRerender() {
      // 强制刷新用户组件
      this.renderComponent = false
      this.$nextTick(() => {
        this.renderComponent = true
      })
    }
  }
}
</script>

<style scoped>
.wrap {
  position: relative;
}
.multipleWrap {
  line-height: unset;
  display: flex;
}
</style>
